package com.keldian.mobilesafe.service;

import android.app.ActivityManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;

import com.keldian.mobilesafe.activity.EnterPwdActivity;
import com.keldian.mobilesafe.dao.ApplockDao;

import java.util.List;

/**
 * Created by Administrator on 16-1-19.
 */
public class WatchDogService extends Service {
    private static final String TAG = "WatchDogService";
    private ActivityManager am;
    private boolean flag;
    private ApplockDao dao;
    private String tempStopProtectPackname;

    private List<String> protectPacknames;
    private Intent intent;
    private InnerReceiver receiver;
    private DataChangeReceiver dataChangeReceiver;

    private ScreenOffReceiver offreceiver;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public void onCreate() {
        am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

        receiver = new InnerReceiver();
        offreceiver = new ScreenOffReceiver();
        dataChangeReceiver = new DataChangeReceiver();
        registerReceiver(dataChangeReceiver, new IntentFilter("com.itheima.mobilesafe.applockchange"));

        registerReceiver(offreceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
        registerReceiver(receiver, new IntentFilter("com.itheima.mobilesafe.tempstop"));
        dao = new ApplockDao(this);
        protectPacknames = dao.findAll();
        flag = true;

        intent = new Intent(getApplicationContext(), EnterPwdActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


        new Thread() {
            @Override
            public void run() {
                while (flag) {
                    List<ActivityManager.RunningTaskInfo> infos = am.getRunningTasks(1);
                    String packname = infos.get(0).topActivity.getPackageName();
//                    Log.i(TAG, packname);
                    if (protectPacknames.contains(packname)) {
                        //enter password interface
                        if (packname.equals(tempStopProtectPackname)) {

                        } else {

                            intent.putExtra("packname", packname);


                            startActivity(intent);
                        }
                    }
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        super.onCreate();

    }

    @Override
    public void onDestroy() {
        flag = false;
        super.onDestroy();
        unregisterReceiver(receiver);
        unregisterReceiver(offreceiver);
        unregisterReceiver(dataChangeReceiver);
        dataChangeReceiver = null;
        offreceiver = null;
        receiver = null;
    }

    private class InnerReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            tempStopProtectPackname = intent.getStringExtra("packname");
        }
    }

    private class ScreenOffReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            tempStopProtectPackname = null;
        }
    }


    private class DataChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            System.out.println("数据库的内容变化了。。。");
            protectPacknames = dao.findAll();
        }
    }
}
